﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

namespace Planetwork.Admin
{
    public partial class HomeDateils : System.Web.UI.Page
    {
        static string conStr = ConfigurationManager.AppSettings["conStr"];
        SqlConnection mycon = new SqlConnection(conStr);
       protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["id"] != null)
            {
                string id = Request["id"];
                string sql = "select *from DefaultImage where id=" + id;
                SqlDataAdapter mydat = new SqlDataAdapter(sql, mycon);
                DataSet myds = new DataSet();
                mycon.Open();
                mydat.Fill(myds);
                mycon.Close();
                if (!IsPostBack)
                {
                    this.title.Text = myds.Tables[0].Rows[0]["Title"].ToString();
                    this.content.Text = myds.Tables[0].Rows[0]["content"].ToString();
                    this.about.Text = myds.Tables[0].Rows[0]["about"].ToString();
                    this.img.ImageUrl = "HomeManager.aspx?imgID=" + id;
                }
            }
            else
            {
                Response.Redirect("HomeManager.aspx");
            }

           
        }

        /// <summary>
        /// 保存Home信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void saveBtn_Click(object sender, EventArgs e)
        {
            HttpPostedFile file = this.image.PostedFile;
            byte[] buffer = new byte[file.ContentLength];
            string imageType = file.ContentType;
            Stream stream = file.InputStream;
            stream.Read(buffer, 0, file.ContentLength);

            string sqlStr = "update DefaultImage set title = @title, Content = @content, About = @about,ImageData = @image,ImageMimeType = @imageType where id="+ Request["id"].ToString();

            SqlCommand mycmd = new SqlCommand(sqlStr, mycon);
            string t = title.Text;
            string t2 = content.Text;
            mycmd.Parameters.AddWithValue("@title", this.title.Text);
            mycmd.Parameters.AddWithValue("@content", this.content.Text);
            mycmd.Parameters.AddWithValue("@about", this.about.Text);
            mycmd.Parameters.AddWithValue("@imageType", imageType);
            mycmd.Parameters.AddWithValue("@image", buffer);

            mycon.Open();
            int i = mycmd.ExecuteNonQuery();
            mycon.Close();

            if (i > 0)
                Response.Write("<script>alert('修改成功!');window.location.href ='HomeManager.aspx'</script>");
            else
                Response.Write("<script>alert('修改失败');</script>");
        }
    }
}